package com.chixing.day14_map.work;

import java.util.*;

public class ProductTest {

    public static void main(String[] args) {
        List<Product> list = new ArrayList<>();
        list.add( new Product("宝洁","洗手粉",18.5));
        list.add( new Product("好迪","洗发水",27.5));
        list.add( new Product("联合利华","肥皂",4.5));
        list.add( new Product("宝洁","牙膏",32.5));
        list.add( new Product("洁利","洗面奶",26.0));
        list.add( new Product("洁利","洗面奶",14.0));
        list.add( new Product("多芬","沐浴露",38.5));
        list.add( new Product("宝洁","洗洁精",3.4));

        Map<String,Double> map = new HashMap<>();
        for (Product i : list){
            double sum = i.getCost();
            String brand = i.getBrand();
            if (map.containsKey(brand)){
                sum += map.get(brand);
            }
            map.put(brand,sum);
        }
        System.out.println(map.entrySet());
        List<Map.Entry<String, Double>> list1 = new ArrayList<>(map.entrySet());
        Collections.sort(list1, new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        System.out.println(list1);





        /*TreeMap<String, Double> sortedMap = new TreeMap<>(map);
        System.out.println(sortedMap);*/
        /*List<Map.Entry<String, Double>> list1 = new ArrayList<>(map.entrySet());

        Collections.sort(list1, new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {

                return o1.getValue().compareTo(o2.getValue());
            }
        });


        for (Map.Entry s : list1) {
            System.out.println(s.getKey()+"--"+s.getValue());
        }*/
    }
}
